package com.assignwork.service.impl;

import com.assignwork.dto.DynamicFormDataDTO;
import com.assignwork.entity.DynamicFormData;
import com.assignwork.mapper.DynamicFormDataMapper;
import com.assignwork.service.IDynamicFormDataService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 实现类
 */
@Service
public class DynamicFormDataServiceImpl extends BaseServiceImpl<DynamicFormDataMapper, DynamicFormData> implements IDynamicFormDataService {

    @Override
    public boolean saveOrUpdateDto(List<DynamicFormDataDTO> data) {
        List<DynamicFormData> collect = data.stream().map(DynamicFormDataDTO::toEtt).collect(Collectors.toList());
        return this.saveOrUpdateBatch(collect);
    }

    @Override
    public boolean saveOrUpdateDto(Long formId, List<DynamicFormDataDTO> data) {
        this.update(
                new UpdateWrapper<DynamicFormData>()
                        .lambda()
                        .set(DynamicFormData::getStatus, 2)
                        .eq(DynamicFormData::getFormId, formId)
                        .eq(DynamicFormData::getStatus, 1)
        );
        return this.saveOrUpdateDto(data);
    }

}